<?php

namespace app\common\library;

class ErrorType
{
    const VERSION = '6.0.5';
    const PARAMS_CAN_NOT_BE_EMPTY = '必填参数不能为空！';
    const UN_LOGIN_OR_TOKEN_OUT_TIME = '未登录或登录已失效！';
    const GET_DATA_FAILED = '获取数据失败！';
    const ADD_DATA_FAILED = '新增数据失败！';
    const DATA_EXIST = '数据已存在，请勿重复操作！';
    const DATA_EXCEPTION = '数据异常，请检查！';
    const ACTION_FAILED = '数据操作失败，请检查！';
    const CUSTOM='自定义错误类型';
    const ACTION_TO_MORE='操作过于频繁，请稍后重试！';
    const CAN_NOT_DELETE_ORIGINAL_DATA='不能删除初始数据！';
    const NO_ADMIN='没有操作权限！';

    protected $type = '';
    protected $msg = '';
    protected $code = '';

    static function getCodeBy($type){
        switch ($type){
            case ErrorType::PARAMS_CAN_NOT_BE_EMPTY:
                $resultCode = 1003;break;
            case ErrorType::UN_LOGIN_OR_TOKEN_OUT_TIME:
                $resultCode = 1004;break;
            case ErrorType::GET_DATA_FAILED:
                $resultCode = 1005;break;
            case ErrorType::ADD_DATA_FAILED:
                $resultCode = 1006;break;
            case ErrorType::DATA_EXIST:
                $resultCode = 1007;break;
            case ErrorType::DATA_EXCEPTION:
                $resultCode = 1008;break;
            case ErrorType::ACTION_FAILED:
                $resultCode = 1009;break;
            case ErrorType::ACTION_TO_MORE:
                $resultCode = 1010;break;
            case ErrorType::CAN_NOT_DELETE_ORIGINAL_DATA:
                $resultCode = 1011;break;
            case ErrorType::NO_ADMIN:
                $resultCode = 1012;break;
            case ErrorType::CUSTOM:
                //这里有待改进
                $resultCode = 0;break;
            default:
                $resultCode = 1000;
        }
        return $resultCode;
    }

    public function __construct($errorType,$msg='',$code=0){
        //初始化
        $this->type = $errorType;
        if ($errorType == ErrorType::CUSTOM){
            $this->msg = $msg;
            $this->code = $code;
        }else{
            $this->msg = $errorType;
            $this->code = self::getCodeBy($errorType);
        }
    }

    public function getObjectString(){
        $data = array('type'=>$this->type,
            'msg'=>$this->msg,
            'code'=>$this->code);
        return json_encode($data);
    }

    /**
     * @return string
     */
    public function getMsg()
    {
        return $this->msg;
    }

    /**
     * 这里用到了反射
     * @return string
     */
    public function getType()
    {
        $typeString = '';
        try{
        $r = new \ReflectionClass(self::class);
        //获取定义的常量
        $dd = $r->getConstants();
        foreach ($dd as $key => $value){
            if ($value == $this->type){
                $typeString = $key;
                $this->type = $typeString;
            }
        }}catch (\Exception $e){
            //这里提示错误，找不到该类型
        }
        return $typeString;
    }

    /**
     * @return int|string
     */
    public function getCode()
    {
        return $this->code;
    }








}
